// 不同对象的数据,通过给dep设置WeakMap -> Map -> Set
let product = {
  price: 20,
  quantity: 3,
},
  total = 0;
const targetMap = new WeakMap();
const effect = () => {
  total = product.price * product.quantity;
};
const track = (target, key) => {
  let depsMap = targetMap.get(target);
  if (!depsMap) {
    depsMap = new Map();
    targetMap.set(target, depsMap);
  }
  let deps = depsMap.get(key);
  if (!deps) {
    deps = new Set();
    depsMap.set(key, deps);
  }
  deps.add(effect);
};

const trigger = (target, key) => {
  let depsMap = targetMap.get(target);
  if (!depsMap) {
    return;
  }
  let deps = depsMap.get(key);
  if (deps) {
    deps.forEach((effect) => {
      effect();
    });
  }
};

track(product, "price");
console.log(total);
product.price = 5;
trigger(product, "price");
console.log(total);
track(product, "quantity");
product.quantity = 5;
trigger(product, "quantity");
console.log(total);
